package com.dhq.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.security.*;

import static cn.hutool.crypto.symmetric.SM4.ALGORITHM_NAME;

public class SM4FileEncrypt {
    static{
        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null){
            //No such provider: BC
            Security.addProvider(new BouncyCastleProvider());
        }
    }

    public static final int KEY_SIZE = 128;

    public static byte[] generateKey() throws Exception {
        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
        kg.init(KEY_SIZE, new SecureRandom());
        return kg.generateKey().getEncoded();
    }


    //生成 Cipher
    public static Cipher generateCipher(int mode, byte[] keyData) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException {
        Cipher cipher = Cipher.getInstance("SM4/ECB/PKCS5Padding", BouncyCastleProvider.PROVIDER_NAME);
        Key sm4Key = new SecretKeySpec(keyData, "SM4");
        cipher.init(mode, sm4Key);
        return cipher;
    }


    //加密文件
    public static void encryptFile(byte[] keyData,String sourcePath,String targetPath){
        //加密文件
        try {
            Cipher cipher = generateCipher(Cipher.ENCRYPT_MODE,keyData);
            CipherInputStream cipherInputStream = new CipherInputStream(new FileInputStream(sourcePath), cipher);
            FileUtil.writeFromStream(cipherInputStream, targetPath);
            IoUtil.close(cipherInputStream);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    //解密文件
    public static void decryptFile(byte[] keyData,String sourcePath,String targetPath){
        //解密文件
        try {
            byte[] bytes = IoUtil.readBytes(new FileInputStream(sourcePath));
            ByteArrayInputStream byteArrayInputStream = IoUtil.toStream(bytes);

            Cipher decryCipher = generateCipher(Cipher.DECRYPT_MODE,keyData);
            CipherOutputStream cipherOutputStream = new CipherOutputStream(new FileOutputStream(targetPath), decryCipher);
            IoUtil.copy(byteArrayInputStream, cipherOutputStream);

            IoUtil.close(byteArrayInputStream);
            IoUtil.close(cipherOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {

        String sp = "D:\\11111.txt";//原始文件
        String dp = "D:\\加密文件.txt";//加密后文件
        String dp2 ="D:\\解密文件.txt";//解密后文件

        String key = "05d986b1141227cb20d46d0b5687c4f5";
        byte[] keyData = ByteUtils.fromHexString(key);
        //加密文件
        encryptFile(keyData,sp,dp);

        //解密文件
        decryptFile(keyData,dp,dp2);
    }
}
